<template>
  <div>

    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @click="dialogVisible=true" v-if="hasAuth('menu:add')">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        stripe
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">

      <el-table-column
          prop="title"
          label="名称"
          sortable
          width="180">
      </el-table-column>
      <el-table-column
          prop="permission"
          label="权限编码"
          sortable
          width="180">
      </el-table-column>

      <el-table-column
          prop="icon"
          label="图标">
      </el-table-column>

      <el-table-column
          prop="type"
          label="类型">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.type === 0">目录</el-tag>
          <el-tag size="small" v-else-if="scope.row.type === 1" type="success">菜单</el-tag>
          <el-tag size="small" v-else-if="scope.row.type === 2" type="info">按钮</el-tag>
        </template>

      </el-table-column>

      <el-table-column
          prop="path"
          label="菜单URL">
      </el-table-column>
      <el-table-column
          prop="component"
          label="菜单组件">
      </el-table-column>
      <el-table-column
          prop="menu_sort"
          label="排序号">
      </el-table-column>
      <el-table-column
          prop="hidden"
          label="状态">
        <template slot-scope="scope">
          <el-tag size="small" v-if="!scope.row.hidden" type="success">正常</el-tag>
          <el-tag size="small" v-else type="danger">隐藏</el-tag>
        </template>

      </el-table-column>
      <el-table-column
          prop="icon"
          label="操作">

        <template slot-scope="scope">
          <el-button type="text" @click="editHandle(scope.row.id)" v-if="hasAuth('menu:update')">编辑</el-button>
          <el-divider direction="vertical"></el-divider>

          <template>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)" v-if="hasAuth('menu:delete')">
              <el-button type="text" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>

        </template>
      </el-table-column>

    </el-table>


    <!--新增对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">

        <el-form-item label="类型" prop="type" label-width="100px">
          <el-radio-group v-model="editForm.type">
            <el-radio :label=0>目录</el-radio>
            <el-radio :label=1>菜单</el-radio>
            <el-radio :label=2>按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上级菜单" prop="pid">
          <treeselect
              placeholder="选择上级菜单"
              v-model="editForm.pid"
              :options="editTableData"
          />
        </el-form-item>

        <el-form-item label="标题" prop="title" label-width="100px">
          <el-input v-model="editForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="名字" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="图标" prop="icon" v-show="editShow.icon" label-width="100px">
          <el-input v-model="editForm.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="path" v-show="editShow.path" label-width="100px">
          <el-input v-model="editForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限编码" prop="permission" v-show="editShow.permission" label-width="100px">
          <el-input v-model="editForm.permission" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单组件" prop="component" v-show="editShow.component" label-width="100px">
          <el-input v-model="editForm.component" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="statu" label-width="100px">
          <el-radio-group v-model="editForm.hidden">
            <el-radio :label='true'>禁用</el-radio>
            <el-radio :label='false'>正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序号" prop="menu_sort" label-width="100px">
          <el-input-number v-model="editForm.menu_sort" :min="1" label="排序号">1</el-input-number>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" @click="submitForm('editForm')">立即创建</el-button>
          <el-button @click="resetForm('editForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>


  </div>
</template>

<script>

import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: "Permission",
  data() {
    return {
      dialogVisible: false,
      editForm: {},
      editFormRules: {
        type: [
          {required: true, message: '请选择类型', trigger: 'blur'}
        ],
        title: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        hidden: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        menu_sort: [
          {required: true, message: '请填入排序号', trigger: 'blur'}
        ]
      },
      tableData: [],
      editTableData: [], // 新增菜单下拉选择用
      // 目录显示 ：图片，地址--》名字，状态，序号 都有
      // 菜单显示 ：图片，地址，权限编码，组件---》名字，状态，序号 都有
      // 按钮显示：权限编码 ---》名字，状态，序号 都有
      editShow: {
        icon: true,
        path: true,
        permission: false,
        component: false,
      }
    }
  },
  mounted() {
    this.getMenuTree()

  },
  methods: {
    getMenuTree() {
      this.$http.get("/permission/menu/all/").then(res => {
        console.log(res.data)
        this.tableData = res.data
      })
      this.$http.get('/permission/menu/menu_list/').then(res => {
        this.editTableData = res.data
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let post_type = this.editForm.id ? this.$http.put : this.$http.post
          post_type('/permission/menu/' + (this.editForm.id ? this.editForm.id + '/' : ''), this.editForm)
              .then(res => {
                this.$message({
                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success',
                  onClose: () => {
                    this.getMenuTree()
                  }
                });

                this.dialogVisible = false
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      this.resetForm(formName)
    },

    editHandle(id) {
      this.$http.get('/permission/menu/' + id + '/').then(res => {
        this.editForm = res.data
        this.dialogVisible = true
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },
    delHandle(id) {
      this.$http.delete("/permission/menu/" + id + '/').then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose: () => {
            this.getMenuTree()
          }
        });

      })
    }
  },
  watch: {
    'editForm.type': {
      handler(newValue, oldValue) {
        if (newValue == 0) { // 目录
          this.editShow.icon = true
          this.editShow.path = true
          this.editShow.permission = false
          this.editShow.component = false
          this.editForm.is_menu = true

        } else if (newValue == 1) {// 菜单
          this.editShow.icon = true
          this.editShow.path = true
          this.editShow.permission = true
          this.editShow.component = true

          this.editForm.is_menu = false
          this.editForm.component = 'Layout'
        } else {// 按钮
          this.editShow.icon = false
          this.editShow.path = false
          this.editShow.permission = true
          this.editShow.component = false

          this.editForm.is_menu = false
          this.editForm.component = null

        }

      }

    }
  },
  components: {
    Treeselect
  }
}
</script>

<style scoped>

</style>